#****************************************************************************
#
# COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.
#
# This file is part of Maxsi Technology.
#
#****************************************************************************

include ../Makefiles/Debugging.mak

# Set the path to the final generated binary.
ifeq ($(APPLICATION_TYPE),Library)
BINNAME   = $(APPLICATION_NAME)$(DLLEXT)
else
BINNAME   = $(APPLICATION_NAME)$(EXEEXT)
endif
BINPATH   = $(BINDIR)/$(BINNAME)

# Set the source folders
SRCDIR    = ../$(APPLICATION_NAME)
RESDIR    = ../$(APPLICATION_NAME)
OBJDIR    = ../$(APPLICATION_NAME)/Release
LINKOBJDIR= $(APPLICATION_NAME)/Release

# Detect all the source files in the source directory and list their object files.
SRC      := $(wildcard $(SRCDIR)/*$(SRCEXT))
APPOBJS  := $(patsubst $(SRCDIR)/%,$(OBJDIR)/%,$(subst $(SRCEXT),$(OBJEXT),$(SRC)))

# Detect all the precompiled header files and list their output.
PCH      := $(APPLICATION_PRECOMPILED_HEADERS)
PCHOBJS  := $(subst $(HEXT),$(PCHEXT),$(PCH))

# Detect all the resource files in the source directory and list their object files.
ifneq ($(UNAME),Linux)
RSRC     := $(wildcard $(RESDIR)/*.rc)
RESOBJS  := $(patsubst $(RESDIR)/%,$(OBJDIR)/%,$(subst $(RCEXT),$(ROBJEXT),$(RSRC)))
else
# We don't use this mysterious shit on Linux!
RSRC =
RESOBJS =
endif

# Set up include dirs.
ifeq ($(UNAME),Linux)
OS_INCLUDE_DIRS  = -I/usr/include/gtk-2.0/
else
OS_INCLUDE_DIRS  =
endif
INCLUDE_DIRS = $(APPLICATION_INCLUDE_DIRS) $(OS_INCLUDE_DIRS)

# Set up preprocessor defines.
PREPROCESSOR_DEFINES = $(APPLICATION_PREPROCESSOR_DEFINES)

# Set the compiler flags.
ifeq ($(UNAME),Linux)
OS_CPPFLAGS = `pkg-config --cflags gtk+-2.0` -fvisibility=hidden -fPIC -fno-exceptions -fcheck-new #-pthread
else
OS_CPPFLAGS =
endif
CPPFLAGS = -Wall -Wno-write-strings $(INCLUDE_DIRS) -c $(PREPROCESSOR_DEFINES) $(OS_CPPFLAGS) $(APPLICATION_CPPFLAGS) $(DEBUG_CPPFLAGS)

# Set the resource compiler flags.
RFLAGS = -O COFF

# Set the linker flags.
ifeq ($(APPLICATION_TYPE),Library)
LSHARED = -shared
else
LSHARED =
endif
ifeq ($(UNAME),Linux)
OS_LFLAGS   = -static-libgcc
else
OS_LFLAGS   =
endif

LFLAGS = $(LSHARED) $(OS_LFLAGS) $(DEBUG_CPPFLAGS)

# Set up the library dependencies.
ifeq ($(UNAME),Linux)
OS_LIBS = `pkg-config --libs gtk+-2.0`
else
OS_LIBS = -mwindows
endif
LIBS = $(APPLICATION_LIBS) $(OS_LIBS)

# Now perform all the actual making.

# Do the main loop that causes everything to be compiled.
all: MakeHeader $(OUTDIR) $(OBJDIR) $(APPOBJS) $(RESOBJS)
	@echo Linking $(BINNAME)...
	@$(CC) $(LFLAGS) -o $(BINPATH) $(APPOBJS) $(RESOBJS) $(LIBS)
	@echo --------------------------

# Display a header that we are now compiling this library.
MakeHeader:
	@echo --------------------------
	@echo Building $(BINNAME)...

# Create the output folder.
$(OUTDIR):
	@-mkdir $(OUTDIR)

# Create the temporary folders for source object files.
$(OBJDIR):
	@-mkdir $(OBJDIR)

# For each precompilable header, compile it!
$(PCHOBJS) : $(PCH)
	@echo Precompiling header $<
	@$(CC) $(CPPFLAGS) $< -o $@

# For each object file we depend on the source file with the same name, compile it! 
$(OBJDIR)/%$(OBJEXT):$(SRCDIR)/%$(SRCEXT) $(PCHOBJS)
	@echo $<
	@$(CC) $(CPPFLAGS) $< -o $@

# For each resource object file we depend on the resource file with the same name, compile it!
$(OBJDIR)/%$(ROBJEXT):$(RESDIR)/%$(RCEXT)
	@echo $<
	@$(WNDRES) --include-dir $(RESDIR) $(RFLAGS) $< -o $@

# To clean, delete the following files.
clean:
	-rm $(APPOBJS) $(RESOBJS) $(BINPATH) $(PCHOBJS)

# To run, execute the generated binary.
run:
	$(OUTDIR)/$(BINNAME)


